package com.alpaak.common.utils.word;

import com.alpaak.common.adapter.ConfSetPropertyHolder;
import com.alpaak.common.enumtype.ErrorCodeEnum;
import com.alpaak.common.exception.BusinessException;
import com.alpaak.common.utils.StringUtil;
import com.alpaak.common.utils.log.Log4jUtil;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @ClassName: WordParse 
 * @Description: word读取解析工具类
 * @author: FengWenJu
 * @date: 2017年11月23日 下午5:20:12
 */
public class WordParse {
	
	private static String word_parse_p1 = ConfSetPropertyHolder.getProperty("word_parse_p1");
	private static String word_parse_p2 = ConfSetPropertyHolder.getProperty("word_parse_p2");
	private static String word_parse_p3 = ConfSetPropertyHolder.getProperty("word_parse_p3");
	private static String word_parse_p4 = ConfSetPropertyHolder.getProperty("word_parse_p4");
	private static String word_parse_p5 = ConfSetPropertyHolder.getProperty("word_parse_p5");
	
	/**
	 * @Title: readWord2003  
	 * @Description: 读取word 2003版本，整个内容转换为字符串
	 * @param @param filePath
	 * @param @return
	 * @param @throws BusinessException     
	 * @return String    
	 * @throws
	 */
	public static String readWord2003(String filePath){
		FileInputStream fis;
		HWPFDocument doc;
		String text = null;
		try {
			File f = new File(filePath);
			fis = new FileInputStream(f);
			doc = new HWPFDocument(fis);
			Range rang = doc.getRange();
			text = rang.text();
			//System.out.println(text);
			fis.close();
		} catch (FileNotFoundException e) {
			Log4jUtil.error(WordParse.class, ErrorCodeEnum.FILE_NOT_FOUND.getErrMsg(), e);
			throw new BusinessException(ErrorCodeEnum.FILE_NOT_FOUND.getErrCode(), ErrorCodeEnum.FILE_NOT_FOUND.getErrMsg());
		} catch (IOException e) {
			Log4jUtil.error(WordParse.class, ErrorCodeEnum.FILE_IO.getErrMsg(), e);
			throw new BusinessException(ErrorCodeEnum.FILE_IO.getErrCode(), ErrorCodeEnum.FILE_IO.getErrMsg());
		} 
		return text;
	}

	/**
	 * @Title: readWord2007  
	 * @Description: 读取word 2007版本，整个内容转换为字符串
	 * @param @param filePath
	 * @param @return
	 * @param @throws BusinessException     
	 * @return String    
	 * @throws
	 */
	public static String readWord2007(String filePath){
		String text = null;
		try {
			OPCPackage oPCPackage = POIXMLDocument.openPackage(filePath);
			XWPFDocument xwpf = new XWPFDocument(oPCPackage);
			POIXMLTextExtractor ex = new XWPFWordExtractor(xwpf);
			text = ex.getText();
			//System.out.println(text);
			oPCPackage.close();
		} catch (FileNotFoundException e) {
			Log4jUtil.error(WordParse.class, ErrorCodeEnum.FILE_NOT_FOUND.getErrMsg(), e);
			throw new BusinessException(ErrorCodeEnum.FILE_NOT_FOUND.getErrCode(), ErrorCodeEnum.FILE_NOT_FOUND.getErrMsg());
		} catch (IOException e) {
			Log4jUtil.error(WordParse.class, ErrorCodeEnum.FILE_IO.getErrMsg(), e);
			throw new BusinessException(ErrorCodeEnum.FILE_IO.getErrCode(), ErrorCodeEnum.FILE_IO.getErrMsg());
		}
		return text;
	}
	
	
	
	public static String[] parseWord2003(String templetPath,String filePath){
		//1.解析模板文件(以所需变量为基准，取出两边的String)
		String text = readWord2003(templetPath);
		String[] split = text.split(StringUtil.escape(word_parse_p1));
		//获取姓名前后字符串
		String head = split[0];
		String subString = StringUtil.subString(text, word_parse_p1, word_parse_p2);
		//获取门诊号前后字符串
		String subString1 = StringUtil.subString(text, word_parse_p3, word_parse_p4);
		String subString2 = StringUtil.subString(text, word_parse_p4, word_parse_p5);
		//2.解析实际报告（将变量两边的部分分割掉，得到两个变量）
		String text2 = readWord2003(filePath);
		String name = StringUtil.subString(text2, head, subString);
		String id = StringUtil.subString(text2, subString1, subString2);
		String[] result = new String[]{name,id};
		return result;
	}
	
	public static String[] parseWord2007(String templetPath,String filePath){
		//1.解析模板文件(以所需变量为基准，取出两边的String)
		String text = readWord2007(templetPath);
		String[] split = text.split(StringUtil.escape(word_parse_p1));
		String head = split[0];
		String subString = StringUtil.subString(text, word_parse_p1, word_parse_p2);
		String subString1 = StringUtil.subString(text, word_parse_p3, word_parse_p4);
		String subString2 = StringUtil.subString(text, word_parse_p4, word_parse_p5);
		//2.解析实际报告（将变量两边的部分分割掉，得到两个变量）
		String text2 = readWord2007(filePath);
		String name = StringUtil.subString(text2, head, subString);
		String id = StringUtil.subString(text2, subString1, subString2);
		String[] result = new String[]{name,id};
		return result;
	}
	
	public static void main(String[] args) {

	String text = readWord2003("E:\\upload\\templet.doc").trim();
	//System.out.println(text);
	String[] split = text.split(" \\[@1\\]");
	String head = split[0].trim();
	String subString = StringUtil.subString(text, "[@1]", "[@4]");
	String name = StringUtil.subString(text, head, subString);
	System.out.println(name);
	
	String subString2 = StringUtil.subString(text, "[@3]", "[@9]");
	String subString3 = StringUtil.subString(text, "[@9]", "[@6]");
	String subString4 = StringUtil.subString(text, subString2, subString3);
	System.out.println(subString4);
	
	String[] split2 = split[1].split(" \\[@9\\]");
	String s = split2[0];
	String end = StringUtil.escape(split2[1]);
	String text2 = readWord2003("E:\\upload\\templet.doc");
	String[] split3 = text2.split(head);
	String[] split4 = split3[1].split(end);
	String[] split5 = split4[0].split(StringUtil.escape(s));
	// System.out.println(end);
//	for (String string : split6) {
//		System.out.println(string.trim());
//		System.out.println("~~~~~~~~~~~~~~~~~~~");
//	}
	//System.out.println(split4[0]);
	}

}
